home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / Timestamp.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  4.4 KB  |  182 lines

  1. package symjava.sql;
  2.  
  3. import java.util.Date;
  4.  
  5. public class Timestamp extends Date {
  6.    private int nanos;
  7.  
  8.    public Timestamp(int year, int month, int date, int hour, int minute, int second, int nano) {
  9.       super(year, month, date, hour, minute, second);
  10.       if (nano > 999999999) {
  11.          throw new IllegalArgumentException("nano > 999999999");
  12.       } else {
  13.          this.nanos = nano;
  14.       }
  15.    }
  16.  
  17.    public Timestamp(long time) {
  18.       super(time / 1000L * 1000L);
  19.       this.nanos = (int)(time % 1000L * 1000000L);
  20.    }
  21.  
  22.    public static Timestamp valueOf(String s) {
  23.       int a_nanos = 0;
  24.       int firstColon = 0;
  25.       int secondColon = 0;
  26.       int period = 0;
  27.       if (s == null) {
  28.          throw new IllegalArgumentException();
  29.       } else {
  30.          String trimmed_s = s.trim();
  31.          int dividingSpace = s.indexOf(32);
  32.          if (dividingSpace > 0) {
  33.             String date_s = s.substring(0, dividingSpace);
  34.             String time_s = s.substring(dividingSpace + 1);
  35.             int firstDash = date_s.indexOf(45);
  36.             int secondDash = date_s.indexOf(45, firstDash + 1);
  37.             if (time_s == null) {
  38.                throw new IllegalArgumentException();
  39.             } else {
  40.                firstColon = time_s.indexOf(58);
  41.                secondColon = time_s.indexOf(58, firstColon + 1);
  42.                period = time_s.indexOf(46, secondColon + 1);
  43.                if (firstDash > 0 & secondDash > 0 & secondDash < date_s.length() - 1) {
  44.                   int year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
  45.                   int month = Integer.parseInt(date_s.substring(firstDash + 1, secondDash)) - 1;
  46.                   int day = Integer.parseInt(date_s.substring(secondDash + 1));
  47.                   if (firstColon > 0 & secondColon > 0 & secondColon < time_s.length() - 1) {
  48.                      int hour = Integer.parseInt(time_s.substring(0, firstColon));
  49.                      int minute = Integer.parseInt(time_s.substring(firstColon + 1, secondColon));
  50.                      int second;
  51.                      if (period > 0 & period < time_s.length() - 1) {
  52.                         second = Integer.parseInt(time_s.substring(secondColon + 1, period));
  53.                         String nanos_s = time_s.substring(period);
  54.                         if (nanos_s.length() > 10) {
  55.                            throw new IllegalArgumentException();
  56.                         }
  57.  
  58.                         a_nanos = (int)Math.round((double)Float.valueOf(nanos_s) * (double)1.0E9F);
  59.                      } else {
  60.                         if (period > 0) {
  61.                            throw new IllegalArgumentException();
  62.                         }
  63.  
  64.                         second = Integer.parseInt(time_s.substring(secondColon + 1));
  65.                      }
  66.  
  67.                      return new Timestamp(year, month, day, hour, minute, second, a_nanos);
  68.                   } else {
  69.                      throw new IllegalArgumentException();
  70.                   }
  71.                } else {
  72.                   throw new IllegalArgumentException();
  73.                }
  74.             }
  75.          } else {
  76.             throw new IllegalArgumentException();
  77.          }
  78.       }
  79.    }
  80.  
  81.    public String toString() {
  82.       int year = super.getYear() + 1900;
  83.       int month = super.getMonth() + 1;
  84.       int day = super.getDate();
  85.       int hour = super.getHours();
  86.       int minute = super.getMinutes();
  87.       int second = super.getSeconds();
  88.       String yearString = "" + year;
  89.       String monthString;
  90.       if (month < 10) {
  91.          monthString = "0" + month;
  92.       } else {
  93.          monthString = Integer.toString(month);
  94.       }
  95.  
  96.       String dayString;
  97.       if (day < 10) {
  98.          dayString = "0" + day;
  99.       } else {
  100.          dayString = Integer.toString(day);
  101.       }
  102.  
  103.       String hourString;
  104.       if (hour < 10) {
  105.          hourString = "0" + hour;
  106.       } else {
  107.          hourString = Integer.toString(hour);
  108.       }
  109.  
  110.       String minuteString;
  111.       if (minute < 10) {
  112.          minuteString = "0" + minute;
  113.       } else {
  114.          minuteString = Integer.toString(minute);
  115.       }
  116.  
  117.       String secondString;
  118.       if (second < 10) {
  119.          secondString = "0" + second;
  120.       } else {
  121.          secondString = Integer.toString(second);
  122.       }
  123.  
  124.       String nanosString;
  125.       if (this.nanos == 0) {
  126.          nanosString = "";
  127.       } else {
  128.          nanosString = Integer.toString(this.nanos);
  129.          char[] nanosChar = new char[nanosString.length()];
  130.          nanosString.getChars(0, nanosString.length(), nanosChar, 0);
  131.  
  132.          int truncIndex;
  133.          for(truncIndex = nanosString.length() - 1; nanosChar[truncIndex] == '0'; --truncIndex) {
  134.          }
  135.  
  136.          nanosString = new String(nanosChar, 0, truncIndex + 1);
  137.       }
  138.  
  139.       return yearString + "-" + monthString + "-" + dayString + " " + hourString + ":" + minuteString + ":" + secondString + "." + nanosString;
  140.    }
  141.  
  142.    public int getNanos() {
  143.       return this.nanos;
  144.    }
  145.  
  146.    public void setNanos(int n) {
  147.       if (n > 999999999) {
  148.          throw new IllegalArgumentException("nano > 999999999");
  149.       } else {
  150.          this.nanos = n;
  151.       }
  152.    }
  153.  
  154.    public boolean equals(Timestamp ts) {
  155.       if (super.equals(ts)) {
  156.          return this.nanos == ts.nanos;
  157.       } else {
  158.          return false;
  159.       }
  160.    }
  161.  
  162.    public boolean before(Timestamp ts) {
  163.       if (super.before(ts)) {
  164.          return true;
  165.       } else if (super.equals(ts)) {
  166.          return this.nanos < ts.nanos;
  167.       } else {
  168.          return false;
  169.       }
  170.    }
  171.  
  172.    public boolean after(Timestamp ts) {
  173.       if (super.after(ts)) {
  174.          return true;
  175.       } else if (super.equals(ts)) {
  176.          return this.nanos > ts.nanos;
  177.       } else {
  178.          return false;
  179.       }
  180.    }
  181. }
  182.